package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.mall.mapper.ShopDetailMapper;
import com.lam.mall.entity.ShopDetail;
import com.lam.mall.param.ShopDetailParam;
import com.lam.mall.service.ShopDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 店铺管理Service业务层处理
 * 
 * @author admin
 * @date 2023-04-27
 */
@Service
public class ShopDetailServiceImpl  implements ShopDetailService {

    @Autowired
    private ShopDetailMapper shopDetailMapper;

    /**
     * 查询店铺管理
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public ShopDetail selectById(String id) {

        return shopDetailMapper.selectById(id);
    }

    /**
     * 查询店铺管理列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<ShopDetail> selectList(ShopDetailParam param) {
    
        QueryWrapper<ShopDetail> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("shop_name", param.getKeywords());
            });
        }
        
        queryWrapper.eq(Objects.nonNull(param.getId()), "id", param.getId());
        queryWrapper.like(StringUtils.isNotBlank(param.getShopNameKey()), "shop_name", param.getShopNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getShopName()), "shop_name", param.getShopName());
        queryWrapper.eq(Objects.nonNull(param.getShopType()), "shop_type", param.getShopType());

        return shopDetailMapper.selectList(queryWrapper);
    }

    /**
     * 新增店铺管理
     * 
     * @param shopDetail 
     * @return 
     */
    @Override
    @Transactional
    public int insert(ShopDetail shopDetail) {
    
        return shopDetailMapper.insert(shopDetail);
    }

    /**
     * 修改店铺管理
     * 
     * @param shopDetail 
     * @return 
     */
    @Override
    @Transactional
    public int update(ShopDetail shopDetail) {
    
        return shopDetailMapper.updateById(shopDetail);
    }

    /**
     * 删除店铺管理信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return shopDetailMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Override
	@Transactional(rollbackFor=Exception.class)
	@CacheEvict(cacheNames = "shop_detail", key = "#shopId")
	public void deleteByShopIdCache(String shopId) {
		shopDetailMapper.deleteById(shopId);
	}

	@Override
	@Cacheable(cacheNames = "shop_detail", key = "#shopId")
	public ShopDetail getByShopIdCache(String shopId) {
		return shopDetailMapper.selectById(shopId);
	}

	@Override
	@CacheEvict(cacheNames = "shop_detail", key = "#shopId")
	public void removeCacheByShopIdCache(String shopId) {
	}
    
}
